package org.example.controller;

import org.example.bean.Order;
import org.example.bean.Product;
import org.example.config.OrderProperties;
import org.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RefreshScope
public class OrderController {

    @RequestMapping(value = "/createOrder")
    public Order createOrder(@RequestParam("userId") String userId, @RequestParam("productId") String productId){
        Order order = new Order();
        order.setUserId(userId);
        order.setName("AA");
        order.setAddress("位置1");
        return order;
    }

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/getOrder")
    public Order getOrder(@RequestParam("userId") String userId, @RequestParam("productId") String productId){
        Order order = new Order();
        order.setUserId(userId);
        order.setName("AA");
        order.setAddress("位置1");
        Product product = orderService.getProduct(productId);
//        Product productRemote01 = orderService.getProductRemote01(productId);
//        Product productRemote02 = orderService.getProductRemote02WithLoad(productId);
//        Product productRemote03 = orderService.getProductRemote03WithLoad(productId);
//        order.setProductList(Arrays.asList(product,productRemote01,productRemote02,productRemote03));

        Product productRemote03 = orderService.getProductRemote03WithLoad(productId);
        order.setProductList(Arrays.asList(product,productRemote03));
        return order;
    }

    @Value("${order.timeout}")
    private String orderTimeout;

    @Value("${order.auto-confirm}")
    private String orderAutoConfirm;

    @Value("${asaGrade}")
    private String asaGrade;

    @Autowired
    private OrderProperties orderProperties;

    @RequestMapping(value = "/configTest")
    public Map<String, Object> configTest(){
        Map<String,Object> map = new HashMap<>();
        map.put("orderTimeout",orderTimeout);
        map.put("orderAutoConfirm",orderAutoConfirm);
        map.put("asaGrade",asaGrade);
        map.put("orderProperties",orderProperties);
        return map;
    }

    @RequestMapping(value = "/getOrderFeign")
    public Order getOrderFeign(@RequestParam("userId") String userId, @RequestParam("productId") String productId){
        Order order = new Order();
        order.setUserId(userId);
        order.setName("Feign测试");
        order.setAddress("位置2");
        Product product = orderService.getProduct(productId);
        Product productRemote01 = orderService.getProductRemote04WithFeign(productId);
        order.setProductList(Arrays.asList(product,productRemote01));
        return order;
    }

    @RequestMapping(value = "/getOther")
    public String getOther( @RequestParam("id") String id){
        return orderService.getOtherInfo(id);
    }

    @RequestMapping(value = "/timeOutTest")
    public Object timeOutTest(){
        return orderService.timeOutTest();
    }

    @RequestMapping(value = "/fallbackTest")
    public Object fallbackTest(){
        return orderService.fallbackTest();
    }

    @RequestMapping(value = "/sentinelTest")
    public Object sentinelTest(@RequestParam("id") String id){
        return orderService.sentinelTest(id);
    }

    @RequestMapping(value = "/sentinelResourceFallbackTest")
    public Object sentinelResourceFallbackTest(@RequestParam("id") String id){
        return orderService.sentinelResourceFallbackTest(id);
    }
}
